#!/usr/bin/env python3
# encoding: utf-8

"""
@version: 1.0
@author: 周永波
@file: Register.py
@time: 2020/8/10 10:25
"""
from flask import Flask
from flask_restful import Api
from flasgger import Swagger
from demoApp.api.UserAction import userAction
from demoApp.api.dbOpt import dbOptViews
from demoApp.api.hello import views, hells

# 接口注册
from demoApp.filter.filters import retHandler, OauthLogin
from demoApp.setting import appConst


class Views:

    def __init__(self, app: Flask) -> None:
        self.app = app
        self.register()
        pass

    def register(self):
        self.app.register_blueprint(views)
        self.app.register_blueprint(dbOptViews)
        self.app.register_blueprint(userAction)

class RestfulViews:
    def __init__(self, api: Api) -> None:
        self.api = api
        self.register()
        pass

    def register(self):
        self.api.add_resource(hells, '/hells')
        pass


# 过滤器注册
class Filter:

    def __init__(self, app: Flask,config={}) -> None:
        self.app = app
        self.config = config
        self.register()
        self.register_template()
        pass

    def register_filter(self, before, after, exception):
        if before is not None:
            self.app.before_request(before)
        if after is not None:
            self.app.after_request(after)
        if exception is not None:
            self.app.teardown_request(exception)
        pass

    def register(self):
        # self.app.add_template_filter()
        # 统一返回值处理
        self.register_filter(retHandler.ret_before, retHandler.ret_after, retHandler.ret_exception)
        # 接入第三方登录
        OauthLogin(self.config)
        self.register_filter(OauthLogin.ret_before, OauthLogin.ret_after, OauthLogin.ret_exception)
        pass

    def register_template(self):
        pass

